﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ourmsg.Server; 

namespace OurmsgServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //FTPServer ftpServer = new FTPServer(@"d:\Ftp\", 21);//Ftp文件服务器

        RouterServer routerServer;//= new  RouterServer(5555);//消息路由器
        AgentServer agentServer;//= new  AgentServer("127.0.0.1", 5555, "1", 8989);//用户登录服务器
        P2PServer P2pServer;// = new P2PServer(14343);//P2P服务器
        RegisterUserServer registerUserServer;//= new RegisterUserServer(800);//用户注册服务器
        ImageFileServer imageFileServer;// = new ImageFileServer(90, 90);//图片服务器
        OfflineFileServer offlineFileServer;// = new OfflineFileServer(88);//离线文件服务器
        OfflineMessageServer offlineMessageServer;// = new OfflineMessageServer();//离线消息存取服务器

        private void butStart_Click(object sender, EventArgs e)
        {
            var SqlIP = ConfigHelper.GetAppConfig("SqlIP");
            var SqlDB = ConfigHelper.GetAppConfig("SqlDB");
            var SqlUser = ConfigHelper.GetAppConfig("SqlUser");
            var SqlPassword = ConfigHelper.GetAppConfig("SqlPassword");

            var MySqlIP = ConfigHelper.GetAppConfig("MySqlIP");
            var MySqlDB = ConfigHelper.GetAppConfig("MySqlDB");
            var MySqlUser = ConfigHelper.GetAppConfig("MySqlUser");
            var MySqlPassword = ConfigHelper.GetAppConfig("MySqlPassword");
             
            int RouterPort = Convert.ToInt32(ConfigHelper.GetAppConfig("RouterPort"));
            int AgentPort = Convert.ToInt32(ConfigHelper.GetAppConfig("AgentPort"));
            int P2pPort = Convert.ToInt32(ConfigHelper.GetAppConfig("P2pPort"));
            int RegisterUserPort = Convert.ToInt32(ConfigHelper.GetAppConfig("RegisterUserPort"));
            int ImageFilePort = Convert.ToInt32(ConfigHelper.GetAppConfig("ImageFilePort"));
            int OfflineFilePort = Convert.ToInt32(ConfigHelper.GetAppConfig("OfflineFilePort"));
            int FtpPort = Convert.ToInt32(ConfigHelper.GetAppConfig("FtpPort"));

            if (routerServer == null)
            routerServer = new RouterServer(RouterPort);//消息路由器
            if (agentServer == null)
                agentServer = new AgentServer("127.0.0.1", RouterPort, "1", AgentPort, P2pPort, ImageFilePort, OfflineFilePort);//用户登录服务器
            if (P2pServer == null)
                P2pServer = new P2PServer(P2pPort);//P2P服务器
            if (registerUserServer == null)
                registerUserServer = new RegisterUserServer(RegisterUserPort);//用户注册服务器
            if (imageFileServer == null)
                imageFileServer = new ImageFileServer(ImageFilePort, ImageFilePort);//图片服务器
            if (offlineFileServer == null)
                offlineFileServer = new OfflineFileServer(OfflineFilePort);//离线文件服务器
            if (offlineMessageServer == null)
                offlineMessageServer = new OfflineMessageServer();//离线消息存取服务器

            //IMLibrary.DBAccess.SetSQLConnection(SqlIP, SqlDB, SqlUser, SqlPassword);//设置sql server数据库连接信息
            IMLibrary.DBAccess.SetMySQLConnection(MySqlIP, MySqlDB, MySqlUser, MySqlPassword);//设置Mysql数据库连接信息
            IMLibrary.DBAccess.DBtype = IMLibrary.DBAccess.DBType.MySQL;//设置要使用的数据库类型

            butStart.Enabled = false;
            butStop.Enabled = true;
            offlineMessageServer.Start();//离线消息服务先启动 
            routerServer.Start();
            agentServer.Start();
            P2pServer.Start();
            registerUserServer.Start();
            imageFileServer.Start();
            offlineFileServer.Start();
            Console.WriteLine("Start OK!");
        }

        private void butStop_Click(object sender, EventArgs e)
        {
            butStart.Enabled = true;
            butStop.Enabled = false;
            agentServer.Stop(); 
            routerServer.Stop();
            P2pServer.Stop();
            registerUserServer.Stop(); 
            imageFileServer.Stop();
            offlineFileServer.Stop();//离线消息服务后停止
            offlineMessageServer.Stop();
            Console.WriteLine("Stop OK!");
        }


    }
}
